/**
*      author: Y_Yao
*      created: 2025-02-22 14:29:06
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 3e5 + 7;
int a[N][2];
int n;
int f[N];//喝解药
int g[N];//喝毒药

void solve()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i][0] >> a[i][1];
    }

    for(int i = 1;i <= n;i++)
    {
        f[i] = f[i-1];
        g[i] = g[i-1];
        if(a[i][0] == 1)//有毒
        {
            g[i] = max(g[i],f[i-1] + a[i][1]);
        }
        else//解药
        {
            f[i] = max(f[i],f[i-1] + a[i][1]);
            f[i] = max(f[i],g[i-1] + a[i][1]);
        }
    }

    cout << max(f[n],g[n]) << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}